/*
 * Copyright 2019-present HiveMQ GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hivemq.extensions.rbac.redis;

import com.hivemq.extension.sdk.api.ExtensionMain;
import com.hivemq.extension.sdk.api.parameter.ExtensionStartInput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStartOutput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStopInput;
import com.hivemq.extension.sdk.api.parameter.ExtensionStopOutput;
import com.hivemq.extension.sdk.api.services.Services;
import com.hivemq.extension.sdk.api.services.auth.SecurityRegistry;
import com.hivemq.extensions.rbac.redis.configuration.ExtensionConfiguration;
import com.hivemq.extensions.rbac.redis.configuration.entities.ExtensionRedisConfig;
import com.hivemq.extensions.rbac.redis.utils.RedisKeyOnecCredentialsValidator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

@SuppressWarnings("unused")
public class RedisAuthMain implements ExtensionMain {

    private static final String NAME = "Redis客户端权鉴";
    private static final String VERSION = "20250207.1";

    private static final @NotNull Logger LOG = LoggerFactory.getLogger(RedisAuthMain.class);

    @Override
    public void extensionStart(
            final @NotNull ExtensionStartInput extensionStartInput,
            final @NotNull ExtensionStartOutput extensionStartOutput) {
        LOG.info("启动插件:{}, 版本号:{}.", NAME, VERSION);
        try {
            final File extensionHomeFolder = extensionStartInput.getExtensionInformation().getExtensionHomeFolder();
            final ExtensionConfiguration extensionConfiguration = new ExtensionConfiguration(extensionHomeFolder);

            final RedisKeyOnecCredentialsValidator credentialsValidator =
                    new RedisKeyOnecCredentialsValidator(extensionConfiguration.getExtensionConfig(),
                                                          Services.metricRegistry());
            credentialsValidator.init();
            // 插件信息
            final ExtensionRedisConfig extensionRedisConfig = extensionConfiguration.getExtensionConfig();
            // 服务注册
            SecurityRegistry securityRegistry = Services.securityRegistry();
            securityRegistry.setAuthenticatorProvider(new RedisAuthenticatorProvider(credentialsValidator,
                    extensionRedisConfig));
        } catch (final Exception e) {
            LOG.error("Exception thrown at extension start: ", e);
        }
    }

    @Override
    public void extensionStop(
            final @NotNull ExtensionStopInput extensionStopInput,
            final @NotNull ExtensionStopOutput extensionStopOutput) {
        LOG.info("停止插件:{}-{}.", NAME,VERSION);
        RedisKeyOnecCredentialsValidator.closeJedisPool();
    }
}
